package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pojo.EmpRole;
import com.example.mapper.EmpRoleMapper;
import com.example.service.IEmpRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.ResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.Min;

/**
 * <p>
 * 员工角色表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-05-05
 */
@Service
public class EmpRoleServiceImpl extends ServiceImpl<EmpRoleMapper, EmpRole> implements IEmpRoleService {

    @Override
    @Transactional
    public ResultVo updateEmpRole(EmpRole empRole) {
        //1.删除原来的角色列表
        int empId = empRole.getEmpId();
        baseMapper.delete(new QueryWrapper<EmpRole>().eq("emp_id", empId));
        //2.添加更新后的角色列表
        empRole.getRoleIds().forEach(e->{
            baseMapper.insert(new EmpRole().setEmpId(empId).setRoleId(e));
        });
        return ResultVo.ok();
    }
}
